/* eslint-disable no-undef */
import zdd from 'gdt-jsapi';
// import getLoginUser from 'gdt-jsapi/getLoginUser';

// 数字千分位
export const amountSlite = (value) => {
  const regExp = /(\d{1,3})(?=(\d{3})+(?:$|\.))/g;
  const ret = (value + '').replace(regExp, '$1,');
  return ret;
};

// 去除数字千分位
export function rmAmountSlite(s) {
  return parseFloat(s.replace(/[^\d\.-]/g, ''));
}

export const conversionBase64 = (blob) => {
  const reader = new FileReader();
  reader.readAsDataURL(blob);
  return new Promise(resolve => {
    reader.onload = ({ target: { result }}) => {
      resolve(result);
    };
  });
};

export const setLocal = locals => {
  if (Array.isArray(locals)) {
    locals.forEach(item => {
      const key = Object.keys(item)[0];
      uni.setStorage({
        key,
        data: item[key]
      });
    });
  } else {
    Object.keys(locals).map(key => {
      const value = locals[key];
      uni.setStorage({
        key,
        data: value
      });
    });
  }
};

export const getLocalStorage = key => {
  return uni.getStorageSync(key);
};

export const removeLocalStorage = key => {
  return uni.removeStorageSync(key);
};

export const Message = msg => {
  uni.showToast({
    title: msg,
    duration: 2000,
    icon: 'none'
  });
};

export const getLoginUserInfo = () => {
  return new Promise(resolve => {
    zdd.ready(() => {
      zdd.getLoginUser().then(res => {
        resolve(res);
      }).catch(err => {
        resolve(err);
      });
    });
  });
};

// 手机环境
export const envirCurr = () => {
  const u = navigator.userAgent;
  const isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1; // g
  const isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); // ios终端
  if (isAndroid) {
    // 这个是安卓操作系统
    return 'isAndroid';
  }
  if (isIOS) {
    // 这个是ios操作系统
    return 'ios';
  }
};

// 获取当前的年月日时分秒
export function getCurrentTime() {
  var datetime = new Date();
  var year = datetime.getFullYear();
  var month = datetime.getMonth() + 1 < 10 ? '0' + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
  var date = datetime.getDate() < 10 ? '0' + datetime.getDate() : datetime.getDate();
  var hour = datetime.getHours() < 10 ? '0' + datetime.getHours() : datetime.getHours(); // 获取系统时，
  var minutes = datetime.getMinutes() < 10 ? '0' + datetime.getMinutes() : datetime.getMinutes(); // 分
  var seconds = datetime.getSeconds() < 10 ? '0' + datetime.getSeconds() : datetime.getSeconds(); // 秒

  const tips = year + '-' + month + '-' + date + ' ' + hour + ':' + minutes + ':' + seconds;
  return tips;
}
